<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>`
<script>
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    let num = arr.slice(1, 5)//开始位置是包含的，结束位置是不包含的
    console.log(num);
    //数组排序
    // arr2.sort()
    // console.log(arr2.sort());
    let arr2 = [2,5,9,6,3,7,1,8,10,1,3,8,3]
    //冒泡排序
    for(let i=0;i<arr2.length-1;i++){
for(let j=0;j<arr2.length-1-i;j++){
    if(arr2[j] < arr2[j+1]){
let temp = arr2[j]//定义中间变量接受较大值
arr2[j] = arr2[j+1]
arr2[j+1]=temp;
    }
}
    }
    console.log(arr2);
    let arr3 = ['a','b',2,3,5,1,7,6,4,33]

    let newArr3 = arr3.sort(function(a,b){
        return a-b//升序
        // return b-a//降序
    })
    console.log(arr3);
    //对象排序 {属性名:属性值}这就是对象
    let obj = {
        name:'张三',
        age:18,
        sex:'女',
        job:'学生',
        height:3.15
            }
            console.log(obj.name);
            let stNum = [
                {
            name:'石页',
            score:0,
            age:2,
            sex:'男'
                },
                {
                    name:'周三金',
                    score:3,
                    age:3,
                    sex:'未知'
                },
                {
                    name:'余日天天',
                    score:1,
                    age:5,
                    sex:'男'
                }
            ]
            stNum.sort(function(a,b){
            //a代表数组的前一项，b代表数组的后一项。
return a.sex-b.sex
//按成绩排序，要选其他的，就该a.后面的数组，只能存在一条排序
            })
            console.log(stNum);
            //数组倒序
            let arr4 = [1,4,3,5,2,6,8,7]
            console.log(arr4);
            arr4.sort().reverse()
            console.log(arr4);
            //map 方法  不会改变原数组，返回值为数组
            let arr5 = [1,3,4,7,2,6]
            let newArr5 = arr5.map(function(item,index,arr){
            //    return 结束循环，返回值·
                return item * 2
                //item:代表数组的每一项(必须)
                //idnex:代表数组的下标(可省略)
                //arr:代表数组的本身(可省略)
                //参数至少拥有一个
                item >=4 ? item : ''
            })
            console.log(newArr5);
    //数组遍历
    let arr6 = [1,2,3,4,5,6]
    for(let i=0;i<arr6.length;i++){
console.log(arr6[i]);
    }
//for in 循环  遍历数组下标
for(index in arr6){
console.log(index,+ arr6[index]);
}
//for/ of 循环数组的每一项
for(item of arr6){
    console.log(item);
}
// forEach 遍历数组
// let sum = 0
// arr6.forEach(function(item,index,arr){
// // console.log(item);
// // console.log(index);
// // console.log(arr);
// if(item % 2 == 0){
//     item * 2 
//     sum += item * 2
// }
// })
// console.log(sum);
let arr6New = []
let arr6back = arr6.forEach(function(item,index){
    if(item % 2 == 0){
        arr6New.push(item) 
    }
})
console.log(arr6New);
//过滤筛选
let  newArrback= arr6.filter(function(item,index,arr){
    // return item>=5
    return item > 2 && item < 5
})
console.log((newArrback));
let students = [
    {
        name:'杨石页',sex:'女'
    },
    {
        name:'杨石页',sex:'男'
    },
    {
        name:'杨石页',sex:'女'
    },
    {
        name:'杨石页',sex:'男'
    },{
        name:'杨石页',sex:'女'
    },{
        name:'杨石页',sex:'男'
    }
]
let newstudens = students.filter(function(item){
    return item.sex == '男'
}
)
console.log(newstudens);
//查询
let arr7=[1,'2','3','4','5','a']
console.log(arr7.indexOf('3'));
console.log(arr7.lastIndexOf('a'));
console.log(arr7.includes('3'));//返回布尔值
console.log(arr7.find(function(a){
    return a == '2'
}));//返回本身
console.log(arr7.findIndex(function (a) {
    return a =='a'
}));//返回下标
//数组转字符串
let arr7str = arr7.join('$')
console.log(arr7str);
//把字符串str升序排列
let str = 'abkcgz35872'
let newStr = str.split('')
console.log(newStr.sort());
let Str = newStr.sort(function(a,b){
return a - b
})
console.log(Str.join(''));
//数组累加方法
let shop = [{name:'牛奶',price:10},
{name:'饼干',price:50},
{name:'牙膏',price:2},
{name:'泡面',price:75},
{name:'拉面',price:100},
{name:'意大利面',price:150}
]
let shopPrice = shop.reduce(function(a,b){
   return a += b.price
},0)
console.log(`商品总价为:${shopPrice}元`);
let arr8 = [22,44,45,77,80,60]
//pre 代表初始值 cur 代表数组每一项
let Num = arr8.reduce(function(pre,cur,index,arr){
return pre += cur 
},0)
console.log(Num);
let sum = 0
for(let i = 0;i < arr8.length;i++){
    sum += arr8[i]
}console.log(sum);
//数组去重
let arr9 = [1,1,1,1,2,2,2,2,3,3,3,8,9,4,1,5,3,5,7]
let arr9back = arr9.reduce(function (pre,cur) {
    if(!pre.includes(cur)){//如果该元素在新数组中不存在
       return pre.concat(cur)//将该元素添加刀新数组中
       pre.push(cur)//返回的是数组的长度
    }else{
        return pre
    }
},[])
console.log(arr9back);

 </script>